package com.markul.android.ui.detail

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.markul.android.MarkulApplication
import com.markul.android.R

class DetailedAdapter(val picList: List<String>) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    inner class MoreViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picImage: ImageView = itemView.findViewById(R.id.imageView)
    }

    inner class OnlyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val picImage: ImageView = itemView.findViewById(R.id.imageView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        if (viewType == 1) {
            view = LayoutInflater.from(MarkulApplication.context)
                .inflate(R.layout.item_detailed_single, parent, false)
            view.setBackgroundColor(0)
            return OnlyViewHolder(view)
        } else {
            view = LayoutInflater.from(MarkulApplication.context)
                .inflate(R.layout.item_detailed_multi, parent, false)
            view.setBackgroundColor(0)
            return MoreViewHolder(view)
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is OnlyViewHolder) {
            Glide.with(holder.itemView.context).load(picList[position])
                .into(holder.picImage)
        } else if (holder is MoreViewHolder) {
            Glide.with(holder.itemView.context).load(picList[position])
                .into(holder.picImage)
        }
    }

    override fun getItemCount() = picList.size

    override fun getItemViewType(position: Int): Int {
        return if (picList.size == 1) {
            1
        } else {
            2
        }
    }
}